﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ReBasic.Helper
{
    public class DictHelper
    {
        public event MainFrame.TripleHandler ProcessEvent;
        public event MainFrame.InfoHandler StateEvent;

        public Dictionary<string, int> SubConDict; //subconcept, index
        public Dictionary<string, int> SupConDict;

        string ProbasePath = @"F:\Relationship_Extraction\"
            + "core47_5.tax";

        StreamWriter sw;
        StreamReader sr;
        FileStream fsr, fsw;
        
        int cnt = 0, sub_cnt = 0, sup_cnt = 0;
            
        #region Events
        void OnProcessEvent(int va, int vb, int perc)
        {
            if (ProcessEvent != null)
                ProcessEvent(va, vb, perc);
        }

        void OnStateEvent(string info, bool isOver)
        {
            if (StateEvent != null)
                StateEvent(info, isOver);
        }
        #endregion

        public void LoadSubConDict()
        {
            if (SubConDict != null && SubConDict.Count > 0)
            {
                OnStateEvent("Dictionary has already been loaded.", true);
                return;
            }
            OnStateEvent("Start loading sub-concept dictionary...", false);
            sub_cnt = 0;
            SubConDict = new Dictionary<string, int>();
            fsr = new FileStream(MainFrame.SubConPath, FileMode.Open);
            sr = new StreamReader(fsr, Encoding.UTF8);
            while (!sr.EndOfStream)
            {
                string str = sr.ReadLine();
                SubConDict.Add(str, sub_cnt);
                if (++sub_cnt % 100000 == 0)
                    OnProcessEvent(sub_cnt, sub_cnt,
                        (int)(100.0 * sub_cnt / MainFrame.TotalSubConcept));
            }
            sr.Close();
            fsr.Close();
            OnStateEvent("Load SubDict complete, with "
                + sub_cnt.ToString() + " distinct sub-concepts.", true);
        }

        public void GenerateSubDict()
        {
            cnt = sub_cnt = 0;
            SubConDict = new Dictionary<string, int>();
            fsr = new FileStream(ProbasePath, FileMode.Open);
            sr = new StreamReader(fsr, Encoding.UTF8);
            fsw = new FileStream(MainFrame.SubConPath, FileMode.Create);
            sw = new StreamWriter(fsw, Encoding.UTF8);
            while (!sr.EndOfStream)
            {
                string str = sr.ReadLine();
                string[] spt = str.Split('\t');
                if (!SubConDict.ContainsKey(spt[1]))
                {
                    SubConDict.Add(spt[1], sub_cnt++);
                    sw.WriteLine(spt[1]);
                }
                if (++cnt % 100000 == 0)
                    OnProcessEvent(cnt, sub_cnt, 0);
            }
            sr.Close();
            fsr.Close();
            sw.Close();
            fsw.Close();
            OnStateEvent("Sub-conceptes load complete, with " 
                + cnt.ToString() + " line of data, "
                + sub_cnt.ToString() + " distinct concepts.", true);
        }

        public void LoadSupConDict()
        {
            if (SupConDict != null && SupConDict.Count > 0)
            {
                OnStateEvent("Dictionary has already been loaded.", true);
                return;
            }
            OnStateEvent("Start loading sup-concept dictionary...", false);
            sup_cnt = 0;
            SupConDict = new Dictionary<string, int>();
            fsr = new FileStream(MainFrame.SupConPath, FileMode.Open);
            sr = new StreamReader(fsr, Encoding.UTF8);
            while (!sr.EndOfStream)
            {
                string str = sr.ReadLine();
                SupConDict.Add(str, sup_cnt);
                if (++sup_cnt % 100000 == 0)
                    OnProcessEvent(sup_cnt, sup_cnt,
                        (int)(100.0 * sup_cnt / MainFrame.TotalSupConcept));
            }
            sr.Close();
            fsr.Close();
            OnStateEvent("Load SupDict complete, with "
                + sup_cnt.ToString() + " distinct sup-concepts.", true);
        }

        public void GenerateSupDict()
        {
            cnt = sup_cnt = 0;
            SupConDict = new Dictionary<string, int>();
            fsr = new FileStream(ProbasePath, FileMode.Open);
            sr = new StreamReader(fsr, Encoding.UTF8);
            fsw = new FileStream(MainFrame.SupConPath, FileMode.Create);
            sw = new StreamWriter(fsw, Encoding.UTF8);
            while (!sr.EndOfStream)
            {
                string str = sr.ReadLine();
                string[] spt = str.Split('\t');
                if (!SupConDict.ContainsKey(spt[0]))
                {
                    SupConDict.Add(spt[0], sup_cnt++);
                    sw.WriteLine(spt[0]);
                }
                if (++cnt % 100000 == 0)
                    OnProcessEvent(cnt, sup_cnt, 0);
            }
            sr.Close();
            fsr.Close();
            sw.Close();
            fsw.Close();
            OnStateEvent("Sup-conceptes load complete, with "
                + cnt.ToString() + " line of data, "
                + sup_cnt.ToString() + " distinct concepts.", true);
        }

        public void CloseFile(int whichWork)
        {
            if (sr != null)
            {
                sr.Close();
                fsr.Close();
            }
            if (sw != null)
            {
                sw.Close();
                fsw.Close();
            }
            if (whichWork == 0)         //Gen Sub Dict
                OnStateEvent("Generateing SubDict stopped, with "
                    + sub_cnt.ToString() + " concepts.", true);
            //else if (whichWork == 1)    //Gen Sent_ds
            //    OnStateEvent("Generating Sent_ds stopped.");
        }
    }
}
